package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> SelectSetmeal() {
        //调用Dao层
        List<Setmeal>list=setMealDao.SelectSetmeal();
        return list;
    }

    /**
     * 手机端--基于套餐id查询套餐详情(套餐的检查组项(检查项))
     *
     * @param id 套餐id
     * @return 返回套餐详情
     */
    @Override
    public Setmeal findByIdSetMeal(Integer id) {
        // (1).基于套餐id查询套餐
        Setmeal setmeal = setMealDao.selectById(id);
        //获取出当前套餐id
        Integer setmealId = setmeal.getId();
        // 基于套餐id查询对应的检查组id集合对象
        List<Integer> groupList = setMealDao.selectByIdGroup(setmealId);
        if (Objects.isNull(groupList)){
            return setmeal;
        }
        //查询检查组信息
        List<CheckGroup> checkGroupList = checkGroupDao.selectByGroup(groupList);
        //遍历检查组信息
        for (CheckGroup checkGroup : checkGroupList) {
            //获取检查项id集合对象
            List<Integer> itemListId = checkItemDao.selectByIdList(checkGroup.getId());
            //获取检查项集合对象
            List<CheckItem> checkItemList = checkItemDao.selectByItemId(itemListId);
            checkGroup.setCheckItems(checkItemList);
        }
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }
}
